package com.tj.template.config.vo;

import com.fqgj.exception.common.ApplicationException;
import com.tj.bo.common.conf.enums.RestCodeEnum;

/**
 * @program: binary-option
 * @description
 * @author: binary
 * @create: 2020-06-08 17:33
 **/
public class RequestLocalInfo {

    private static final ThreadLocal<UserAccessInfo> currentUser = new ThreadLocal<>();
    private static final ThreadLocal<RequestBasicInfo> currentRequestBasicVO = new ThreadLocal<>();


    public static void clearThreadLocal() {
        currentUser.remove();
        currentRequestBasicVO.remove();
    }

    public static void putCurrentUser(UserAccessInfo user) {
        if (user == null) {
            throw new ApplicationException(RestCodeEnum.USER_NOT_EXIST_ERROR);
        }

        RequestLocalInfo.currentUser.set(user);
    }

    public static UserAccessInfo getCurrentUser() {
        return currentUser.get();
    }

    public static RequestBasicInfo getRequestBasicInfo() {
        RequestBasicInfo requestBasicInfo = currentRequestBasicVO.get();
        return requestBasicInfo;
    }

    /**
     * 当前请求的app参数信息
     *
     * @param requestBasicVO
     */
    public static void putCurrentRequestBasicVO(RequestBasicInfo requestBasicVO) {
        if (requestBasicVO == null) {
            return;
        }
        RequestLocalInfo.currentRequestBasicVO.set(requestBasicVO);
    }
}
